package com.dinadi.yidi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MessageRequestEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/21
 * Time: 12:47
 */

public class HistoryRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<String> mDatas = new ArrayList<>();
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String historyRecord);
    }

    public void addAllData(List<String> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    public HistoryRecordAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_history, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new HistoryRecordAdapter.ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.id_index_gallery_item_tv.setText(mDatas.get(position).toString());
        holder.itemView.setTag(mDatas.get(position).toString());

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView id_index_gallery_item_tv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            id_index_gallery_item_tv = (TextView) itemView.findViewById(R.id.id_index_gallery_item_tv);
            //  initListener(itemView);
        }

        private void initListener(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }
}

